package Olivia.olivia3d.model.ms3d
{
	import Olivia.olivia3d.model.AnimationMat;
	import Olivia.olivia3d.vo.Ms3dVo;
	import Olivia.olivia3d.vo.Stage3DVo;

	public class TempSimplifyModel
	{
		public function TempSimplifyModel()
		{
		}
		
		public static function reusemodel(msvo:Ms3dVo,ms3dstr:Ms3dStruct):void
		{
			var trianglesArr:Array = [];
			var vertexArr:Array = [];
			var normalArr:Array = [];			
			if(ms3dstr.nJoints > 0){
				msvo.type = 1;
				msvo.dataPerVertex = 6;
				msvo.jointData = dealJoint(ms3dstr.Joints);				
			}
			for(var i:int=0; i < ms3dstr.nTriangles; ++i){
				var tempTri:MsTriangle = ms3dstr.Triangles[i];
				for(var j:int=0; j < tempTri.vertexIndices.length; ++j){
					var idx:int = tempTri.vertexIndices[j];
//					trianglesArr.push(idx);
					var s:Number = tempTri.s[j];
					var t:Number = tempTri.t[j];
										
					vertexArr[idx*msvo.dataPerVertex] = ms3dstr.Vertices[idx].x;
					vertexArr[idx*msvo.dataPerVertex+1] = ms3dstr.Vertices[idx].y;
					vertexArr[idx*msvo.dataPerVertex+2] = ms3dstr.Vertices[idx].z;
					if(msvo.type == Ms3dVo.BaseType){
						vertexArr[idx*msvo.dataPerVertex+3] = s;
						vertexArr[idx*msvo.dataPerVertex+4] = t;
					}else if(msvo.type == Ms3dVo.AnimType){
						vertexArr[idx*msvo.dataPerVertex+3] = ms3dstr.Vertices[idx].boneId;
//						trace(ms3dstr.Vertices[idx].boneId);
						vertexArr[idx*msvo.dataPerVertex+4] = s;
						vertexArr[idx*msvo.dataPerVertex+5] = t;					
					}
					
					normalArr[idx*3] = tempTri.vertexNormals[j][0];					
					normalArr[idx*3+1] = tempTri.vertexNormals[j][1];					
					normalArr[idx*3+2] = tempTri.vertexNormals[j][2];					
				}
			}
			msvo.vertexData = Vector.<Number>(vertexArr);
			msvo.normalData = Vector.<Number>(normalArr);
			msvo.triangleData = ms3dstr.Triangles;
			msvo.groupData = ms3dstr.Groups;
			msvo.materialData = ms3dstr.Materials;
			msvo.fAnimationFPS = ms3dstr.fAnimationFPS;
			msvo.fCurrentTime = ms3dstr.fCurrentTime;
			msvo.iTotalFrames = ms3dstr.iTotalFrames;
		}
		
		private static function dealJoint(joints:Vector.<MsJoint>):Vector.<MsJointEx>
		{
			var jexs:Vector.<MsJointEx> = new Vector.<MsJointEx>();
			var idx:int=0;
			for each(var j:MsJoint in joints){
				var tempMsJex:MsJointEx = new MsJointEx();				
				tempMsJex.idx = idx;
				idx++;
				tempMsJex.flags = j.flags;
				tempMsJex.name = j.name;
				tempMsJex.parentName = j.parentName;
				//init
				tempMsJex.initAMat = new AnimationMat();
				tempMsJex.initAMat.setupParentToLocal(j.rotation,j.position);
				tempMsJex.abAMat = new AnimationMat(
					tempMsJex.initAMat.orm.Mat.concat(
						[tempMsJex.initAMat.tx,tempMsJex.initAMat.ty,tempMsJex.initAMat.tz]));
				//frames
				var num:int = j.numKeyFramesRot;
				tempMsJex.KFAmats = new Vector.<AnimationMat>(num);
				for(var i:int=0; i < num; ++i){
					var rot:Array = [j.KeyFramesRots[i][1],j.KeyFramesRots[i][2],j.KeyFramesRots[i][3]];
					var pos:Array = [j.KeyFramesTrans[i][1],j.KeyFramesTrans[i][2],j.KeyFramesTrans[i][3]];					
					tempMsJex.KFAmats[i] = new AnimationMat();
					tempMsJex.KFAmats[i].setupLocalToParent(rot,pos);
					tempMsJex.KFAmats[i].append(AnimationMat.inverse(tempMsJex.initAMat));//L*A-1
				}
				jexs.push(tempMsJex);
			}
			dealfps(jexs);
			return jexs;
		}
		
		private static function dealfps(jexs:Vector.<MsJointEx>):void
		{
			for(var i:int=0; i < jexs.length; ++i){
				var jex:MsJointEx = jexs[i];				
				for(var j:int=0; j < jexs.length; ++j){
					var jex1:MsJointEx = jexs[j];
					if(jex1.parentName == jex.name){
						//pinit*init
						jex1.abAMat.prepend(jex.abAMat);//PA*A
						//all fps
						for(var k:int=0; k < jex1.KFAmats.length; ++k){
							jex1.KFAmats[k].append(jex.KFAmats[k]);//L*A-1*(PL*PA-1)
						}
					}
				}
			}
			
			//final matrix = abMat * kfaniMat
			for each(var jex2:MsJointEx in jexs){				
				for each(var aniMat:AnimationMat in jex2.KFAmats){
					aniMat.prepend(jex2.abAMat);
				}
			}
		}
		
		public static function createMs3dIndexBuffer(s3dvo:Stage3DVo,msvo:Ms3dVo):void
		{
			for each(var g:MsGroup in msvo.groupData){
				var idxArr:Array = [];				
				for each(var vidx:uint in g.triangleIndices){
					idxArr = idxArr.concat(msvo.triangleData[vidx].vertexIndices);
				}
				var indexs:Vector.<uint> = Vector.<uint>(idxArr);
				g.indexBuffer = s3dvo.context3D.createIndexBuffer(indexs.length);
				g.indexBuffer.uploadFromVector(indexs,0,indexs.length);
			}
			msvo.initGroupIdx = true;
		}
	}
}